package com.example.study.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;

/**
 * @Author 宋伟宁
 * @Date 2023/10/8
 * @Version 1.0
 **/
@Configuration
@EnableWebMvc //开启webmvc
@ComponentScan("com.example.study")
@MapperScan("com.example.study.mapper")
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/list")
                .setViewName("/index");

    }

    //    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//         registry.addInterceptor(new onte(),)
//                 .addPathPatterns("/**")
//                 .excludePathPatterns("/webjars/**");
//    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
      configurer.enable();
    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/webjars/**")
//                .addResourceLocations("classpath:/META-INF/resources/webjars/");
////        registry.addResourceHandler("/static/**")
////                .addResourceLocations("classpath:/static/");
//    }

    @Bean
    public SpringResourceTemplateResolver springResourceTemplateResolver(){
        SpringResourceTemplateResolver springResourceTemplateResolver =
                new SpringResourceTemplateResolver();
        //设置模板模型为html
        springResourceTemplateResolver.setTemplateMode("html");
        //设置模板的排序为1（首选为html）
        springResourceTemplateResolver.setOrder(1);
        //设置静态页面的前缀
        springResourceTemplateResolver.setPrefix("classpath:/templates/");
        //设置页面的后缀为.html
        springResourceTemplateResolver.setSuffix(".html");
        //设置页面编码
        springResourceTemplateResolver.setCharacterEncoding("utf-8");
        return  springResourceTemplateResolver;
    }
    @Bean
    public SpringTemplateEngine springTemplateEngine(){
        SpringTemplateEngine templateEngine= new SpringTemplateEngine();
        templateEngine.setTemplateResolver(springResourceTemplateResolver());
        return  templateEngine;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver(){
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
        thymeleafViewResolver.setTemplateEngine(springTemplateEngine());
        thymeleafViewResolver.setCharacterEncoding("utf-8");
        thymeleafViewResolver.setOrder(1);
        return thymeleafViewResolver;
    }
}
